package cn.cttic.dashboard.controller;

import cn.cttic.common.core.domain.R;
import cn.cttic.dashboard.domain.*;
import cn.cttic.dashboard.service.IWorkbenchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工作台接口
 * 前端访问路由地址为:/dashboard/workbench
 *
 * @author Times.Studio
 * @date 2024-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workbench")
public class WorkbenchController {

    private final IWorkbenchService workbenchService;

    /**
     * 查询指标数据
     *
     * @return 指标数据
     */
    @GetMapping("/getFullBenchmark")
    R<FullBenchmarkVo> getFullBenchmark() {
        return R.ok(workbenchService.getFullBenchmark());
    }

    /**
     * 查询销售排名
     *
     * @return 销售排名
     */
    @GetMapping("/getSaleRank")
    R<List<SaleRankVo>> getSaleRank() {
        return R.ok(workbenchService.getSaleRank());
    }

    /**
     * 查询安装排名
     *
     * @return 安装排名
     */
    @GetMapping("/getInstallRank")
    R<List<InstallRankVo>> getInstallRank() {
        return R.ok(workbenchService.getInstallRank());
    }

    /**
     * 查询验收排名
     *
     * @return 验收排名
     */
    @GetMapping("/getVerifyRank")
    R<List<VerifyRankVo>> getVerifyRank() {
        return R.ok(workbenchService.getVerifyRank());
    }

    /**
     * 查询产品服务统计数据
     *
     * @return 产品服务统计数据
     */
    @GetMapping("/getProductCounterStatistic")
    R<List<ProductCounterStatisticVo>> getProductCounterStatistic() {
        return R.ok(workbenchService.getProductCounterStatistic());
    }

    /**
     * 查询产品销售年度分布数据
     *
     * @return 产品销售年度分布数据
     */
    @GetMapping("/getProductSaleByMonthOverYear")
    R<ProductSaleByMonthOverYearDisplayVo> getProductSaleByMonthOverYear(@RequestParam(value = "productId") String productId) {
        return R.ok(workbenchService.getProductSaleByMonthOverYear(productId));
    }

    /**
     * 查询销售安装情况分布
     *
     * @return 销售安装情况分布
     */
    @GetMapping("/getSaleInstallByDayOverPeriod")
    R<List<SaleInstallByDayOverPeriodChartVo>> getSaleInstallByDayOverPeriod(@RequestParam(value = "periodType") Integer periodType) {
        LocalDateTime endTime = null;
        LocalDateTime startTime = null;
        if (periodType == null || periodType <= 0) {
            endTime = LocalDate.now().atTime(LocalTime.MAX);
            startTime = LocalDate.now().minusMonths(1).atTime(LocalTime.MIN);
        } else {
            LocalDate localDate = LocalDate.now().minusMonths(periodType - 1);
            endTime = localDate.with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
            startTime = localDate.with(TemporalAdjusters.firstDayOfMonth()).atTime(LocalTime.MIN);
        }
        return R.ok(workbenchService.getSaleInstallByDayOverPeriod(startTime, endTime));
    }

    /**
     * 查询作业流年度统计分析
     *
     * @return 作业流年度统计分析
     */
    @GetMapping("/getWorkStatisticByMonthOverYear")
    R<WorkStatisticByMonthOverYearDisplayVo> getWorkStatisticByMonthOverYear(@RequestParam(value = "year") Integer year) {
        return R.ok(workbenchService.getWorkStatisticByMonthOverYear(year));
    }


}
